package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.MyCountTimer;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 修改支付密码
 * 
 * @author zk
 *
 */
public class ModifyPayPwd01Activity extends BaseActivity {

	private static final String ClassName = ModifyPayPwd01Activity.class.getName();

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	/** 当前手机号 */
	private TextView notify_tel_num;
	/** 输入验证码 */
	private EditText notify_code_input;
	/** 获取验证码 */
	private TextView notify_get_code;
	/** 确定 */
	private Button notify_btn;

	private String curPhone;

	private MyCountTimer timer;

	@Override
	public int layoutResID() {
		return R.layout.activity_notify_telnumber;
	}

	@Override
	public void findView() {
		findTitleIds();

		notify_tel_num = V.f(this, R.id.notify_tel_num);
		notify_code_input = V.f(this, R.id.notify_code_input);
		notify_get_code = V.f(this, R.id.notify_get_code);
		notify_btn = V.f(this, R.id.notify_btn);
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("修改支付密码");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		notify_get_code.setOnClickListener(this);
		notify_btn.setOnClickListener(this);

		timer = new MyCountTimer(notify_get_code, 0xfff30008, 0xff969696);
		//curPhone = PrefUtils.getString(ModifyPayPwd01Activity.this, "userPhone", "");
		//notify_tel_num.setText(curPhone.substring(0,3)+"****"+curPhone.substring(7));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.notify_get_code:// 获取验证码
			// showToast("获取验证码");
			timer.start();
			httpGetCodes(curPhone);
			break;
		case R.id.notify_btn:// 确定
			String code = notify_code_input.getText().toString();
			if(TextUtils.isEmpty(code)){
				showToast("请输入验证码");
				return;
			}
			
			verificationCode(curPhone,code);
			
			break;
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		httpGetPhone();
	}
	
	
	private void httpGetPhone() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				JSONObject info = new JSONObject();
				try {
					info.put("uid", PrefUtils.getString(ModifyPayPwd01Activity.this, "userId", ""));
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				JSONObject json = new JSONObject();
				try {
					json.put("model", "MemberInfo");
					json.put("function", "index");
					json.put("info", info);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				RequestParams params = new RequestParams();
				try {
					params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				HttpUtils utils = new HttpUtils();
				utils.configCookieStore(Values.cookieStore);
				utils.send(HttpMethod.POST, OApi.Appport_appMember, params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						try {
							JSONObject jo = new JSONObject(result);
							if(jo.getInt("status") == 1){
								JSONObject data = jo.getJSONObject("data");
								curPhone = data.getString("phone");
								notify_tel_num.setText(curPhone.substring(0,3)+"****"+curPhone.substring(7));
								//item_safe_text.setText(phone.substring(0,3)+"****"+phone.substring(7));
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}

	private void verificationCode(final String phone, String code) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		try {
			json.put("phone", phone);
			json.put("code", code);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.Appport_backCode, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						Intent intent = new Intent(ModifyPayPwd01Activity.this, ModifyPayPwd02Activity.class);
						intent.putExtra("code", notify_code_input.getText().toString());
						intent.putExtra("curPhone", curPhone);
						startActivity(intent);
					}else{
						showToast("验证码不正确！");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	/** 获取验证码 */
	private void httpGetCodes(String phone) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("phone", phone);
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_sendPhoneAppUp, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				try {
					JSONObject o = new JSONObject(arg0.result);
					String data = o.getString("data");
					showToast(data);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		});
	}

	/**
	 * 修改手机号
	 * 
	 * @param oldPone
	 * @param code
	 * @param uid
	 * @param newPhone
	 */
	private void notifyPhone(String oldPone, String code, String uid, String newphone) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("phone", oldPone);
			jsonObject.putOpt("code", code);
			jsonObject.putOpt("uid", uid);
			jsonObject.putOpt("newphone", newphone);
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_updatePhone, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				try {
					JSONObject o = new JSONObject(arg0.result);
					String data = o.getString("data");
					showToast(data);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		});
	}

}
